﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RN.ERP.Web.OrderService;
using RN.ERP.Web.ModelService;
using Fly.Common.Linq;
using Newtonsoft.Json;
using Telerik.Web.UI;

namespace RN.ERP.Web.CA
{
    public partial class CAOrderAudit : BasePage
    {
        OrderServiceClient client = new OrderServiceClient();

        private void BindData()
        {
            RadGrid1.AllowCustomPaging = true;
            int pageCount = RadGrid1.CurrentPageIndex;
            int pageSize = RadGrid1.PageSize;
            QueryInvoiceInfoCondition queryCondition = new QueryInvoiceInfoCondition();

            if (!string.IsNullOrEmpty(txtCorpName.Text.Trim()))
            {
                queryCondition.CorpName = new ParameterValue<string>() { V = txtCorpName.Text.Trim(), O = OptEnum.Like };
            }
            if (!string.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                queryCondition.Code = new ParameterValue<string>() { V = txtCode.Text.Trim(), O = OptEnum.Like };
            }
            if (ddlStatus.SelectedValue == "1")
            {
                queryCondition.bCAStatus = new ParameterValue<byte>() { V = 3, O = OptEnum.GreaterThanOrEqual };
                queryCondition.FlowCode = new ParameterValue<string>() { V = "0,019,029,", O = OptEnum.Like };
            }
            else
            {
                queryCondition.bCAStatus = new ParameterValue<byte>() { V = 3, O = OptEnum.Equal };
                queryCondition.FlowCode = new ParameterValue<string>() { V = ddlStatus.SelectedValue, O = OptEnum.Like };
            }
            if (!string.IsNullOrEmpty(hidGroup.Value))
            {
                queryCondition.GroupCode = new ParameterValue<string>() { V = hidGroup.Value.ToUpper(), O = OptEnum.Equal };
            }
            if (!string.IsNullOrEmpty(hidAreaCode.Value))
            {
                queryCondition.AreaCode1 = new ParameterValue<string>() { V = hidAreaCode.Value, O = OptEnum.StartWith };
            }
            queryCondition.IsAudit = new ParameterValue<bool?>() { V = true, O = OptEnum.Equal };
            List<QueryInvoiceInfoCondition> queryList = new List<QueryInvoiceInfoCondition>();
            queryList.Add(queryCondition);
            int resultCount = client.GetInvoicesCount(JsonConvert.SerializeObject(queryList));
            List<ModelService.CaPayRelation> result = JsonConvert.DeserializeObject<List<ModelService.CaPayRelation>>(client.GetInvoicesList(JsonConvert.SerializeObject(queryList), pageCount, pageSize));
            RadGrid1.VirtualItemCount = resultCount;
            RadGrid1.DataSource = result;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidGroup.Value = Request.QueryString["group"];
                hidAreaCode.Value = Request.QueryString["areacode"];
            }
        }

        protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            BindData();
        }

        protected void RadGrid1_temDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            string str = "";
            int index = 0;
            int intRowSpan = 1;
            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                if (i == index)
                {
                    str = RadGrid1.Items[i].Cells[2].Text;
                    for (int j = i + 1; j < RadGrid1.Items.Count; j++)
                    {
                        if (RadGrid1.Items[j].Cells[2].Text == str)
                        {
                            intRowSpan++;
                            RadGrid1.Items[i].Cells[2].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[2].Visible = false;
                            index++;
                        }
                        else if (j == RadGrid1.Items.Count - 1)
                        {
                            intRowSpan = 1;
                            index++;
                        }
                    }
                }
            }
            index = 0;
            intRowSpan = 1;
            str = "";
            for (int i = 0; i < RadGrid1.Items.Count; i++)
            {
                if (i == index)
                {
                    Label lbId = (Label)RadGrid1.Items[i].Cells[3].FindControl("lbId");
                    str = lbId.Text;
                    for (int j = i + 1; j < RadGrid1.Items.Count; j++)
                    {
                        Label lbId2 = (Label)RadGrid1.Items[j].Cells[3].FindControl("lbId");
                        if (lbId2.Text == str)
                        {
                            intRowSpan++;
                            RadGrid1.Items[i].Cells[3].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[3].Visible = false;
                            RadGrid1.Items[i].Cells[4].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[4].Visible = false;
                            RadGrid1.Items[i].Cells[5].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[5].Visible = false;
                            RadGrid1.Items[i].Cells[6].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[6].Visible = false;
                            RadGrid1.Items[i].Cells[7].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[7].Visible = false;
                            RadGrid1.Items[i].Cells[8].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[8].Visible = false;
                            RadGrid1.Items[i].Cells[9].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[9].Visible = false;
                            RadGrid1.Items[i].Cells[10].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[10].Visible = false;
                            RadGrid1.Items[i].Cells[11].RowSpan = intRowSpan;
                            RadGrid1.Items[j].Cells[11].Visible = false;
                            index++;
                        }
                        else if (j == RadGrid1.Items.Count - 1)
                        {
                            intRowSpan = 1;
                            index++;
                        }
                    }
                }
            }
        }

        protected void lbSearch_Click(object sender, EventArgs e)
        {
            RadGrid1.CurrentPageIndex = 0;
            BindData();
            RadGrid1.DataBind();
        }
    }
}